package com.higgins.client.tabs;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.higgins.client.GreetingService;
import com.higgins.client.GreetingServiceAsync;
import com.higgins.client.components.CoveringLetterColumns;
import com.higgins.client.components.CoveringLetterTable;
import com.higgins.client.components.LawFirmColumns;
import com.higgins.client.components.LawFirmTable;
import com.higgins.client.components.StrategyColumns;
import com.higgins.client.components.StrategyTable;
import com.higgins.client.dataModel.LawFirmTypeDTO;
import com.higgins.client.dataModel.SectorDTO;

public class LawFirmTab extends VerticalPanel {
    private final GreetingServiceAsync greetingService = GWT
                .create(GreetingService.class);
    private LawFirmColumns lawFirmColumns = null;
    private LawFirmTable lawFirmTable = null;
    private ListBox sectorListBox = new ListBox();
    private ListBox lawFirmTypeListBox = new ListBox();
    private List<SectorDTO> sectorList = null;
    private List<LawFirmTypeDTO> lawFirmTypeList = null;
    private HorizontalPanel rowPanel = new HorizontalPanel();
	private boolean firstRun = true;
	public LawFirmTab()	{
	}
	public void update()	{
		if (firstRun)
			initialise();
		else
			refresh();
	}
	public void initialise()	{
        rowPanel.setSpacing(3);
        add(rowPanel);
        loadSectorListBox();
        loadLawFirmTypeListBox();
        attachSectorListBoxHandler();
        attachLawFirmTypeListBoxHandler();
	}
	
	private void initialiseTable() {
		lawFirmColumns = new LawFirmColumns();
		lawFirmTable = new LawFirmTable(lawFirmColumns);
		ScrollPanel scrollPanel = new ScrollPanel();
		scrollPanel.setHeight("600px");
		scrollPanel.add(lawFirmTable);
		add(scrollPanel);
		firstRun = false;
	}
	public void refresh()	{
		
	}
    private void loadSectorListBox()    {
        rowPanel.add(sectorListBox);
        greetingService.retrieveSectors(new AsyncCallback<List<SectorDTO>>()    {
            @Override
            public void onFailure(Throwable caught) {
            }

            @Override
            public void onSuccess(List<SectorDTO> result) {
                sectorList = result;
                sectorListBox.addItem("All");
                for (SectorDTO dto:result)  {
                    sectorListBox.addItem(dto.getName());
                }
                sectorListBox.setSelectedIndex(0);
                checkLoadComplete();
            }});
    }
    private void loadLawFirmTypeListBox()   {
        rowPanel.add(lawFirmTypeListBox);
        greetingService.retrieveLawFirmTypes(new AsyncCallback<List<LawFirmTypeDTO>>()    {
            @Override
            public void onFailure(Throwable caught) {
            }

            @Override
            public void onSuccess(List<LawFirmTypeDTO> result) {
                lawFirmTypeList = result;
                lawFirmTypeListBox.addItem("All");
                for (LawFirmTypeDTO dto:result)  {
                    lawFirmTypeListBox.addItem(dto.getName());
                }
                lawFirmTypeListBox.setSelectedIndex(0);
                checkLoadComplete();
            }});
    }
    private void checkLoadComplete()    {
        if (sectorListBox.getItemCount()>0 && lawFirmTypeListBox.getItemCount()>0)
            initialiseTable();
    }	
    private void updateTable()  {
        lawFirmTable.update(getSector(),
                getLawFirmType());
    }
    private SectorDTO getSector()   {
        int index = sectorListBox.getSelectedIndex();
        if (index <=0)  {
            return null;
        }
        return sectorList.get(index-1);
    }
    private LawFirmTypeDTO getLawFirmType()   {
        int index = lawFirmTypeListBox.getSelectedIndex();
        if (index <=0)  {
            return null;
        }
        return lawFirmTypeList.get(index-1);
    }
    
    private void attachSectorListBoxHandler()   {
        sectorListBox.addChangeHandler(new ChangeHandler()  {

            @Override
            public void onChange(ChangeEvent event) {
                updateTable();
            }
            
        });
        
    }
    private void attachLawFirmTypeListBoxHandler()   {
        lawFirmTypeListBox.addChangeHandler(new ChangeHandler()  {

            @Override
            public void onChange(ChangeEvent event) {
                updateTable();
            }
            
        });
        
    }

    
}
